import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:convert';


class SingleLetter extends StatelessWidget{

  String token;
  String backs;
  String words;
  int code;
  var now = new DateTime.now();
  Response response;

  SingleLetter(this.token,this.backs,this.words);

  _addDiary(){
    Dio().post("http://121.196.101.7:8080/treehole/diary/addDiary",
        options:Options(headers: {"Token":this.token},),queryParameters:{
       "title":"${now.year.toString()}-${now.month.toString().padLeft(2,'0')}-${now.day.
       toString().padLeft(2,'0')} ${now.hour.toString().padLeft(2, '0')}:"
           "${now.minute.toString().padLeft(2, '0')}:${now.second.toString().
       padLeft(2, '0')}","content":this.words,
        } ).then((value) {
      response=value;
      print(response.data);
      code=response.data["code"];
      if(code==0){
        Fluttertoast.showToast(
          msg: "成功保存日记",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,);
      }else{
        Fluttertoast.showToast(
          msg: "保存失败，请重试",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,);
      }
    });
  }


  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: new Text('保存日记',style:TextStyle(color: Colors.black)),
        backgroundColor: Colors.white70,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body: Container(
        decoration: BoxDecoration(
            color: Colors.amber[50]
        ),
        child: Column(
          children: [
            Expanded(
                flex: 1,
                child: SizedBox()),
            Expanded(
              flex: 10,
              child: Center(
                child: Container(
                  width: 300,
                  height: 500,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          fit: BoxFit.cover,
                          image: AssetImage(this.backs)//获取信纸
                      )
                  ),
                  child: Column(
                    children: [
                      Expanded(
                          flex:1,
                          child: SizedBox()),
                      Expanded(
                        flex: 2,
                        child:  Container(
                          margin: EdgeInsets.only(left: 10),
                          alignment: Alignment.centerLeft,
                          child: Text("亲爱的自己：",
                            style: TextStyle(
                                color: Colors.black
                            ),),
                        ),
                      ),
                      Expanded(
                        flex: 19,
                        child:  Container(
                          margin: EdgeInsets.only(left: 10),
                          alignment: Alignment.topLeft,
                          child: Text(this.words,
                            style: TextStyle(
                                color: Colors.black
                            ),),
                        ),
                      ),
                      Expanded(
                          flex: 4,
                          child: Container(
                            margin: EdgeInsets.only(right: 10),
                            alignment: Alignment.bottomRight,
                            child: Column(
                              children: [
                                Expanded(
                                    flex: 2,
                                    child: Text("一个小可爱",textAlign: TextAlign.center,)),
                                Expanded(
                                    flex: 1,
                                    child: SizedBox()),
                                Expanded(
                                    flex: 2,
                                    child: Text("${now.year.toString()}-${now.month.toString().padLeft(2,'0')}-${now.day.
                                    toString().padLeft(2,'0')} ${now.hour.toString().padLeft(2, '0')}:"
                                        "${now.minute.toString().padLeft(2, '0')}:${now.second.toString().
                                    padLeft(2, '0')}")
                                )
                              ],
                            ),
                          )),
                      Expanded(
                          flex: 1,
                          child: SizedBox())
                    ],
                  ),
                ),
              ),),
            Expanded(
                flex: 1,
                child: SizedBox()),
            Expanded(
                flex: 1,
                child: RaisedButton(
                  child: Text("保存日记"),
                  color: Colors.greenAccent[100],
                  elevation: 25,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  ),
                  onPressed: () {
                    _addDiary();
                    Navigator.pop(context);
                    Navigator.pop(context);
                    Navigator.pop(context);//popUtil改起来太麻烦了╮(╯﹏╰）╭，这样写不知道以后会不会有bug
                  },
                )),
            Expanded(
                flex: 1,
                child: SizedBox()),
          ],
        ),
      ),
    );
  }
}